package gui;

import geodata.BorefieldData;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import record.ProjectRecord;
import system.Client;

/**
 * The final window of the design process.  Displays the final results of
 * the calculations performed.
 * @author Stephan
 *
 */
@SuppressWarnings("serial")
public class GeothermalDesignWindow extends JPanel implements ActionListener
{
	/**
	 * The client through which this window will communicate with the server
	 */
	@SuppressWarnings("unused")
	private Client client;
	/**
	 * The currently active prject record
	 */
	private ProjectRecord record;
	/**
	 * The application that is displaying this window
	 */
	private Application app;
	
//	private ButtonGroup buttons;
//	private JRadioButton[] boreArrangements;
	
	/**
	 * The OK button
	 */
	private JButton doneButton;
	/**
	 * The class which performs the geothermal system calculations
	 */
	private BorefieldData borefieldData;
	/**
	 * The field that displays the length of the heating bore
	 */
	private JTextField heatingLength;
	/**
	 * The field that displays the length of the cooling bore
	 */
	private JTextField coolingLength;
	/**
	 * The field that displays the number of heating bores
	 */
	private JTextField heatingBores;
	/**
	 * The field that diplays the number of cooling bores
	 */
	private JTextField coolingBores;
	
	/**
	 * Creates this window and adds it to the application
	 * @param record the currently active project record
	 * @param app the application currently displaying this window
	 */
	public GeothermalDesignWindow(ProjectRecord record, Application app)
	{
		this.client = app.getClient();
		this.record = record;
		this.app = app;
		
		borefieldData = new BorefieldData(record.getBuildingData(), record.getGeoData(),
				record.getBoreData());
		doneButton = new JButton("Done");
		doneButton.addActionListener(this);
		
		heatingLength = new JTextField();
		coolingLength = new JTextField();
		heatingBores = new JTextField();
		coolingBores = new JTextField();
		
		heatingLength.setEditable(false);
		coolingLength.setEditable(false);
		heatingBores.setEditable(false);
		coolingBores.setEditable(false);
		
		JLabel heatingLengthLabel = new JLabel("Heating Bore Length:");
		JLabel coolingLengthLabel = new JLabel("Cooling bore Length:");
		JLabel heatingBoresLabel = new JLabel("Heating Bores:");
		JLabel coolingBoresLabel = new JLabel("Cooling Bores:");
		
		JPanel inputPanel = new JPanel(new GridLayout(4, 2, 5, 5));
		
		inputPanel.add(heatingLengthLabel);
		inputPanel.add(heatingLength);
		inputPanel.add(coolingLengthLabel);
		inputPanel.add(coolingLength);
		inputPanel.add(heatingBoresLabel);
		inputPanel.add(heatingBores);
		inputPanel.add(coolingBoresLabel);
		inputPanel.add(coolingBores);
		
		this.add(inputPanel);
		this.add(doneButton);
		
		initValues();
		
		
//		buttons = new ButtonGroup();
//		boreArrangements = new JRadioButton[10];
//		
//		JPanel buttonPanel = new JPanel(new GridLayout(2, 5, 5, 5));
//		buttonPanel.setBorder(new TitledBorder("Bore Arrangements"));
//		
//		// create 10 JOption buttons with text in the form:
//		// {2|3} x [2,6]
//		// i.e. 2x2, 2x3, ... 3x6
//		for(int i = 0; i < boreArrangements.length; i++)
//		{
//			JRadioButton button = null;
//			if(i < 5)
//			{
//				button = new JRadioButton("2 x " + (i + 2));
//			}
//			else
//			{
//				button = new JRadioButton("3 x " + (i - 3));
//			}
//			button.addActionListener(this);
//			boreArrangements[i] = button;
//			buttonPanel.add(button);
//			buttons.add(button);		
//		}
//		
//		this.add(buttonPanel);
		
		app.getContentPane().removeAll();
		app.getContentPane().add(this);
		app.setVisible(true);
	}
	
	/**
	 * Fills the text fields with the calculated values
	 */
	private void initValues()
	{
		heatingLength.setText("" + borefieldData.heatingLength());
		coolingLength.setText("" + borefieldData.coolingLength());
		heatingBores.setText("" + borefieldData.heatingBores());
		coolingBores.setText("" + borefieldData.coolingBores());		
	}

	/**
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource().equals(doneButton))
		{
			new ProjectMenuWindow(record, app);
		}
//		else if(e.getSource() instanceof JRadioButton)
//		{
//			for(JRadioButton b : boreArrangements)
//			{
//				if(b.equals(e.getSource()) && b.isSelected())
//				{
//					// TODO: update project record & display?
//					JOptionPane.showMessageDialog(this, 
//							((JRadioButton)e.getSource()).getText());
//				}
//			}
//		}
		
	}

}
